package com.busdepot.webapp.domain;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("shippingService")
public class ShippingServiceImpl implements ShippingService {

	@Autowired()
	ShippingRepository shippingRepository;

	public double getCodCharge() {
		return shippingRepository.getCodCharge();
	}

	public double getHandlingCharge() {
		return shippingRepository.getHandlingCharge();
	}

	public double getShippingRate(int shippingRegion, int shippingType) {
		return shippingRepository.getShippingRate(shippingRegion, shippingType);
	}

	public int getShippingRegion(int zip) {
		List<ShippingRegion> regionList = shippingRepository
				.getShippingRegion();

		int regionid = 0;
		for (ShippingRegion region : regionList) {
			if (zip >= region.getStartZip() && zip <= region.getEndZip()) {
				regionid = region.getRegion();
			}
		}
		return regionid;
	}

	public double getShippingTotal(double merchandiseTotal,
			double shippingRate, double surchargeTotal, double handlingCharge,
			double CodCharge) {
		double shippingTotal = merchandiseTotal * shippingRate
				+ merchandiseTotal + surchargeTotal + handlingCharge
				+ CodCharge;
		return shippingTotal;
	}

	public double getSurchargeForPart(BusDepotPart part) {
		int partId = part.getId();
		int vendorId = part.getVendorId();
		double partSurcharge = shippingRepository.getPartSurcharge(partId);
		double categorySurcharge = shippingRepository
				.getCategorySurcharge(partId);
		double vendorSurcharge = shippingRepository
				.getVendorSurcharge(vendorId);
		double surchargeTotal = partSurcharge + categorySurcharge
				+ vendorSurcharge;
		return surchargeTotal;
	}

	public double getSurchargeForPart(ImcPart part) {
		double vendorSurcharge = shippingRepository.getVendorSurcharge(part
				.getVendorId());
		return vendorSurcharge;
	}

	public double getSurchargeTotal(List<BusDepotPart> partList) {
		double surchargeTotal = 0;
		for (BusDepotPart part : partList) {
			double partSurcharge = getSurchargeForPart(part);
			surchargeTotal = surchargeTotal + partSurcharge;
		}
		return surchargeTotal;
	}

}
